package com.sicheng.lc.周赛.分类.模拟.矩阵行走;

import com.sicheng.algorithm.node.ListNode;

import java.util.Arrays;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/7/3 10:52
 */
public class 螺旋矩阵IV {
    //https://leetcode.cn/contest/weekly-contest-300/problems/spiral-matrix-iv/
    static int[][] d = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

    public int[][] spiralMatrix(int m, int n, ListNode head) {
        int[][] g = new int[m][n];
        for (int[] gg : g) {
            Arrays.fill(gg, -1);
        }
        // 右下左上四个方向走
        int i = 0, j = 0;
        ListNode t = head;
        int k = 0;
        int x, y = 0;
        g[i][j] = t.val;
        while (t.next != null) {
            x = i + d[k][0];
            y = j + d[k][1];
            while (t.next != null && x >= 0 && x < m && y >= 0 && y < n && g[x][y] == -1) {
                t = t.next;
                g[x][y] = t.val;
                i = x;
                j = y;
                x = i + d[k][0];
                y = j + d[k][1];
            }
            k = (k + 1) % 4;
        }

        return g;
    }
}
